/*
图书馆类模板
 */
//fixme 暂不可用
import {DA_HELPER,URI} from './dataAbilityHelperModel';
import dataAbility from '@ohos.data.dataAbility';
import prompt from '@ohos.prompt';

const TAG:string = 'LibraryModel';

class Library_basic{
    public tableName:string;
    public columns:string[];
    constructor(name:string,columns:string[]){
        this.tableName = name;
        this.columns = columns;
    }
}

export class Library_root extends Library_basic{
    public static instance:Library_root;
    public length:number;
    public institutionName:string[];
    public subTableName:string[];
    /**
     * 初始化，向数据库查询信息
     */
    public static get():Library_root{//初始化
        if(Library_root.instance == null){
            Library_root.instance = new Library_root();
            let predicates;
            predicates = new dataAbility.DataAbilityPredicates();
            DA_HELPER.query(
                URI.Library_root_URI,
                ['institutionName','tableName'],
                predicates,
                (err,resultSet)=>{
                    if (err.code) {
                        console.info(TAG+":: library1 Query failed, err: " + JSON.stringify(err));
                        return;
                    }
                    console.info(TAG+':: library1 Query result:'+JSON.stringify(resultSet));
                    if(resultSet.rowCount){
                        console.info(TAG+":: library1 Query successful,the root existed");
                        this.instance.length = resultSet.rowCount;
                        resultSet.goToFirstRow();
                        do{
                            this.instance.institutionName.push(resultSet.getString(0));
                            this.instance.subTableName.push(resultSet.getString(1));
                        }while(resultSet.goToNextRow());
                        resultSet.close();
                    }
                    else{
                        resultSet.close();
                        console.info(TAG+"::library1 Query successful,the root not existed");
                        prompt.showToast({message:'场馆管理：数据查询结果异常'})
                    }
                },
            )
        }
        return Library_root.instance;
    }
    constructor(){
        super('Level1_root',['institutionName','tableName']);
        this.length = 0;
        this.institutionName = [];
        this.subTableName = [];
    }
    getSubTable():Library_Level2[]{
        let list:Library_Level2[] = [];
        for(let x in this.subTableName){
            list.push(new Library_Level2(this.subTableName[x]));
        }
        return list;
//        return [
//            {
//                tableName:'l2_wut',
//                columns:['libraryName','tableName'],
//                length:5,
//                libraryName:['wut library1','wut library2','wut library3','wut library4','wut library5'],
//                subTableName:['l3_library1','l3_library2','l3_library3','l3_library4','l3_library5'],
//            },
//        ];
    }
}

export class Library_Level2 extends Library_basic{
    public length:number;
    public libraryName:string[];
    public subTableName:string[];
    constructor(tableName:string){
        super(tableName,['libraryName','tableName']);
        this.length = 0;
        this.libraryName = [];
        this.subTableName = [];
        this.get();
    }
    /**
     * 向数据库查询信息
     */
    get(){
        let predicates;
        predicates = new dataAbility.DataAbilityPredicates();
        DA_HELPER.query(
            URI.generateLibraryUri(this.tableName,2),
            this.columns,
            predicates,
            (err,resultSet)=>{
                if(err.code){
                    console.info(`${TAG}:: library2 ${this.tableName} Query failed, err: ` + JSON.stringify(err));
                    return;
                }
                if(resultSet.rowCount){
                    this.length = resultSet.rowCount;
                    resultSet.goToFirstRow();
                    do{
                        this.libraryName.push(resultSet.getString(0));
                        this.subTableName.push(resultSet.getString(1));
                    }while(resultSet.goToNextRow())
                }
                else{
                    console.info(`${TAG}:: library2 ${this.tableName} Query successful,but rowCount:`+resultSet.rowCount);
                }
            }
        )
    }
    getSubTable():Library_Level3[]{
        let list:Library_Level3[];
        for(let x in this.subTableName){
            list.push(new Library_Level3(this.subTableName[x]));
        }
        return list;
//        return [
//            {
//                tableName:'l3_library1',
//                columns:['bookshelfBlockId','position','note','tableName','usedSpace'],
//                length:5,
//                bookshelfBlockId:['010101','020102','030103','040104','050105'],
//                position:['一楼北区自东向西第1排','二楼北区自东向西第1排',
//                '三楼北区自东向西第1排','四楼北区自东向西第1排','五楼北区自东向西第1排'],
//                note:['','','','',''],
//                subTableName:['bookshelf','bookshelf','bookshelf','bookshelf','bookshelf'],
//                usedSpace:['1/20','1/20','1/20','1/20','1/20'],
//            },
//            {
//                tableName:'l3_library2',
//                columns:['bookshelfBlockId','position','note','tableName','usedSpace'],
//                length:5,
//                bookshelfBlockId:['010101','020102','030103','040104','050105'],
//                position:['一楼北区自东向西第1排','二楼','三楼','四楼','五楼'],
//                note:['','','','',''],
//                subTableName:['bookshelf1','bookshelf2','bookshelf3','bookshelf4','bookshelf5'],
//                usedSpace:['1/20','1/20','1/20','1/20','1/20'],
//            },
//            {
//                tableName:'l3_library3',
//                columns:['bookshelfBlockId','position','note','tableName','usedSpace'],
//                length:5,
//                bookshelfBlockId:['010101','020102','030103','040104','050105'],
//                position:['一楼北区自东向西第1排','二楼','三楼','四楼','五楼'],
//                note:['','','','',''],
//                subTableName:['bookshelf1','bookshelf2','bookshelf3','bookshelf4','bookshelf5'],
//                usedSpace:['1/20','1/20','1/20','1/20','1/20'],
//            },
//            {
//                tableName:'l3_library4',
//                columns:['bookshelfBlockId','position','note','tableName','usedSpace'],
//                length:5,
//                bookshelfBlockId:['010101','020102','030103','040104','050105'],
//                position:['一楼北区自东向西第1排','二楼','三楼','四楼','五楼'],
//                note:['','','','',''],
//                subTableName:['bookshelf1','bookshelf2','bookshelf3','bookshelf4','bookshelf5'],
//                usedSpace:['1/20','1/20','1/20','1/20','1/20'],
//            },
//            {
//                tableName:'l3_library5',
//                columns:['bookshelfBlockId','position','note','tableName','usedSpace'],
//                length:5,
//                bookshelfBlockId:['010101','020102','030103','040104','050105'],
//                position:['一楼北区自东向西第1排','二楼','三楼','四楼','五楼'],
//                note:['','','','',''],
//                subTableName:['bookshelf1','bookshelf2','bookshelf3','bookshelf4','bookshelf5'],
//                usedSpace:['1/20','1/20','1/20','1/20','1/20'],
//            },
//        ];
    }
}

export class Library_Level3 extends Library_basic{
    public length:number;
    /**
     * 6位编号,前4位表示书架,后两位表示分块
     */
    public bookshelfBlockId:string[];
    public position:string[];
    public note:string[];
    public subTableName:string[];
    public usedSpace:string[];
    constructor(tableName:string){
        super(tableName,['bookshelfBlockId','position','note','tableName','usedSpace']);
        this.length = 0;
        this.bookshelfBlockId = [];
        this.position = [];
        this.note = [];
        this.subTableName = [];
        this.usedSpace = [];
        this.get();
    }
    /**
     * 向数据库查询信息
     */
    get(){
        let predicates;
        predicates = new dataAbility.DataAbilityPredicates();
        DA_HELPER.query(
            URI.generateLibraryUri(this.tableName,3),
            this.columns,
            predicates,
            (err,resultSet)=>{
                if(err.code){
                    console.info(`${TAG}:: library3 ${this.tableName} Query failed, err: ` + JSON.stringify(err));
                    return;
                }
                if(resultSet.rowCount){
                    this.length = resultSet.rowCount;
                    resultSet.goToFirstRow();
                    do{
                        this.bookshelfBlockId.push(resultSet.getString(0));
                        this.position.push(resultSet.getString(1));
                        this.note.push(resultSet.getString(2));
                        this.subTableName.push(resultSet.getString(3));
                        this.usedSpace.push(resultSet.getString(4));
                    }while(resultSet.goToNextRow())
                }
                else{
                    console.info(`${TAG}:: library3 ${this.tableName} Query successful,but rowCount:`+resultSet.rowCount);
                }
            }
        )
    }
    /**
     * 返回一个Library_Level4对象
     */
    getSubTable():Library_Level4{
        return new Library_Level4(this.subTableName[0]);//todo tableName:book
    }
}

export class Library_Level4 extends Library_basic{
    public length:number;
    public bookId:string[];
    public bookSortId:string[];
    public inBookshelfBlock:string[];
    public positionRank:string[];
    public status:string[];
    constructor(tableName:string){
        super(tableName,['bookId','bookSortId','inBookshelfBlock','positionRank','status']);
        this.length = 0;
        this.bookId = [];
        this.bookSortId = [];
        this.inBookshelfBlock = [];
        this.positionRank = [];
        this.status = [];
        this.get();
    }
    /**
     * 向数据库查询信息
     */
    get(){
        let predicates;
        predicates = new dataAbility.DataAbilityPredicates();
        DA_HELPER.query(
        URI.generateLibraryUri(this.tableName,3),
            this.columns,
            predicates,
            (err,resultSet)=>{
                if(err.code){
                    console.info(`${TAG}:: library4 ${this.tableName} Query failed, err: ` + JSON.stringify(err));
                    return;
                }
                if(resultSet.rowCount){
                    this.length = resultSet.rowCount;
                    resultSet.goToFirstRow();
                    do{
                        this.bookId.push(resultSet.getString(0));
                        this.bookSortId.push(resultSet.getString(1));
                        this.inBookshelfBlock.push(resultSet.getString(2));
                        this.positionRank.push(resultSet.getString(3));
                        this.status.push(resultSet.getString(4));
                    }while(resultSet.goToNextRow())
                }
                else{
                    console.info(`${TAG}:: library4 ${this.tableName} Query successful,but rowCount:`+resultSet.rowCount);
                }
            }
        )
    }
}